package com.binge.backend.service.impl;

import com.binge.backend.dao.BookshopMapper;
import com.binge.backend.dao.CartMapper;
import com.binge.backend.dao.UserMapper;
import com.binge.backend.dto.BookDto;
import com.binge.backend.entity.BookEntity;
import com.binge.backend.service.IBookshopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author binge
 * @Description 图书商店业务逻辑实现类
 * @date 2023年10月01日 下午 8:50
 */
@Service
@Slf4j
@Transactional
public class BookshopServiceImpl implements IBookshopService {
    @Resource
    private  BookshopMapper bookshopMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CartMapper cartMapper;


    @Override
    public List<BookEntity> getBook(BookDto bookDto,int pageNum) {
        PageHelper.startPage(pageNum,5);
        List<BookEntity> bookEntities = bookshopMapper. getBook(
                bookDto.getBookName () , bookDto.getPublisher());
        PageInfo<BookEntity> pageInfo = new PageInfo<>(bookEntities);
        return pageInfo.getList();

    }

    public List<BookEntity> getBook(BookDto bookDto) {
        List<BookEntity> bookEntities = bookshopMapper.getBook(
                bookDto.getBookName() , bookDto.getPublisher());
        log.info(bookEntities.toString());
        return bookEntities;

    }

    @Override
    public Boolean buyBook(String bookName, String username) {
        int bookId = bookshopMapper. getBookIdByName(bookName);
        int userId = userMapper.getUserIdByName(username);
        int result = cartMapper.selectOrderCarBook(userId , bookId);
        if (result != 0) {
            cartMapper. updateBookNum(userId , bookId);
        }else {
            cartMapper.insertBookByName(userId , bookId);
        }
        return true;


    }
}
